package com.taplinker.client.im.util;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;

import com.taplinker.client.R;

public class SmileyParser {
	private Context mContext;
	public static final int PERPAGE_IMAGE_SIZE = 18;
	private String[] mSmileyTexts = {"<1>", "<2>", "<3>", "<4>", "<5>", "<6>",
			"<7>", "<8>", "<9>", "<10>", "<11>", "<12>", "<13>", "<14>",
			"<15>", "<16>", "<17>", "-77-",

			"<18>", "<19>", "<20>", "<21>", "<22>", "<23>", "<24>", "<25>",
			"<26>", "<27>", "<28>", "<29>", "<30>", "<31>", "<32>", "<33>",
			"<34>", "-77-",

			"<35>", "<36>", "<37>", "<38>", "<39>", "<40>", "<41>", "<42>",
			"<43>", "<44>", "<45>", "<46>", "<47>", "<48>", "<49>", "<50>",
			"<51>", "-77-",

			"<52>", "-77-" };

	private HashMap<String, Integer> mSmileyToRes;

	public static final int[] DEFAULT_SMILEY_RES_IDS = {
			R.drawable.face_1, R.drawable.face_2, R.drawable.face_3, R.drawable.face_4,
			R.drawable.face_5, R.drawable.face_6, R.drawable.face_7, R.drawable.face_8,
			R.drawable.face_9, R.drawable.face_10, R.drawable.face_11, R.drawable.face_12,
			R.drawable.face_13, R.drawable.face_14, R.drawable.face_15, R.drawable.face_16,
			R.drawable.face_17, R.drawable.face_delete,
			
			R.drawable.face_18, R.drawable.face_19, R.drawable.face_20, R.drawable.face_21,
			R.drawable.face_22, R.drawable.face_23, R.drawable.face_24, R.drawable.face_25,
			R.drawable.face_26, R.drawable.face_27, R.drawable.face_28, R.drawable.face_29,
			R.drawable.face_30, R.drawable.face_31, R.drawable.face_32, R.drawable.face_33,
			R.drawable.face_34, R.drawable.face_delete,
			
			R.drawable.face_35, R.drawable.face_36, R.drawable.face_37, R.drawable.face_38,
			R.drawable.face_39, R.drawable.face_40, R.drawable.face_41, R.drawable.face_42,
			R.drawable.face_43, R.drawable.face_44, R.drawable.face_45, R.drawable.face_46,
			R.drawable.face_47, R.drawable.face_48, R.drawable.face_49, R.drawable.face_50,
			R.drawable.face_51, R.drawable.face_delete,
			
			R.drawable.face_52, R.drawable.face_delete };

	private Pattern mPattern;

	public SmileyParser(Context context) {
		mContext = context;

		mSmileyToRes = buildSmileyToRes();

		mPattern = buildPattern();
	}

	private HashMap<String, Integer> buildSmileyToRes() {
		if (DEFAULT_SMILEY_RES_IDS.length != mSmileyTexts.length) {
			throw new IllegalStateException("Smiley resource ID/text mismatch");
		}

		HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(
				mSmileyTexts.length);
		for (int i = 0; i < mSmileyTexts.length; i++) {
			smileyToRes.put(mSmileyTexts[i], DEFAULT_SMILEY_RES_IDS[i]);
		}

		return smileyToRes;
	}

	/**
	 * 构建正则表达式
	 * @return
	 */
	private Pattern buildPattern() {
		StringBuilder patternString = new StringBuilder(mSmileyTexts.length * 3);

		patternString.append('(');
		for (String s : mSmileyTexts) {
			patternString.append(Pattern.quote(s));
			patternString.append('|');
		}
		patternString.replace(patternString.length() - 1, patternString.length(), ")");

		return Pattern.compile(patternString.toString());
	}

	/**
	 * 根据文本替换成表情图片
	 * @param text
	 * @return
	 */
	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	public CharSequence replace(CharSequence text) {
		SpannableStringBuilder builder = new SpannableStringBuilder(text);

		Matcher matcher = mPattern.matcher(text);
		while (matcher.find()) {
			int resId = mSmileyToRes.get(matcher.group());
			Drawable drawable = mContext.getResources().getDrawable(resId);
			drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2, drawable.getIntrinsicHeight()/2);
			ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
			builder.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}

		return builder;
	}
	
	/**
	 * 表情替换为replaceText
	 * @param text
	 * @param replaceText
	 * @return
	 */
	public CharSequence replaceText(CharSequence text, String replaceText) {
		Matcher matcher = mPattern.matcher(text);
		if (matcher.find()) {
			return replaceText;
		}
		return text;
	}

}